package com.chenxi.vhr.service.security;

import com.chenxi.vhr.bean.BaseUser;
import com.chenxi.vhr.mapper.BaseUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author : zhangchenxi
 * @create : 2022-01-24
 **/
@Service
@Transactional
public class NasUserDetailService implements UserDetailsService {

    @Autowired
    BaseUserMapper baseUserMapper;

    /**
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        BaseUser baseUser = (BaseUser) baseUserMapper.loadUserByUsername(username);
        if (baseUser == null) {
            throw new UsernameNotFoundException("用户名" + username + "不存在！");
        }
        baseUser.setRoleList(baseUserMapper.getRoleByUserId(baseUser.getId()));
        return baseUser;
    }
}
